package cst.neu.yuejian_31.model;

/**
 * Created by Alienware on 2017/9/27.
 */

import cst.neu.yuejian_31.bean.LoginBean;
import cst.neu.yuejian_31.iface.LoginIface;
import cst.neu.yuejian_31.iface.LoginListener;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.LoginService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 */

public class LoginModel implements LoginIface {

    private Retrofit retrofit;

//    private String BASEURL = "http://10.0.2.2/neu16_yuejian/api/";

    public LoginModel(){
        retrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    @Override
    public void getResult(String username, String password, final LoginListener loginListener) {
        LoginService loginService = retrofit.create(LoginService.class);
        Call<LoginBean> call = loginService.login(username, password);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
//                String userid=response.body().getUser_id();
                if(response.isSuccessful()&&response!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                loginListener.onFail(t.toString());
            }
        });
    }
}